package MazeGenProject;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.List;

import org.jhotdraw.framework.Drawing;
import org.jhotdraw.framework.DrawingView;
import org.jhotdraw.framework.FigureEnumeration;
import org.jhotdraw.framework.HandleEnumeration;
import org.jhotdraw.standard.CompositeFigure;
import org.jhotdraw.standard.HandleEnumerator;
import org.jhotdraw.standard.RelativeLocator;
import org.jhotdraw.util.CollectionsFactory;

public class SolveMaze extends CompositeFigure {
	
	private GridDrawing _view;
	private Room[][] mazeRooms;
	private boolean[][] solution;
	private int height;
	private int width;
	
	public SolveMaze(GridDrawing view, RandomMaze ranMaze){
		_view = view;
		mazeRooms = ranMaze.getRooms();
		
		height = 20;
		width = 20;
		
		solution = new boolean[20][20];
		
		solveMaze(0,0);
		drawSolution();
	}
	
	public boolean solveMaze(int x, int y)
	{
		int x2 = 19;
		int y2 = 19;
		
		//Mark as part of solution
		solution[y][x] = true;
		
		//If at end, finish
		if (x == x2 && y == y2)
		{
			return true;
		}
		
		//Recursively check for solution moving left, right, down, and up
		if (x > 0 && mazeRooms[y][x-1].east.isGone == true && solveMaze(x-1,y))
		{
			return true;
		}
		if (x < width && mazeRooms[y][x+1].west.isGone == true && solveMaze(x+1, y))
		{
			return true;
		}
		if (y > 0 && mazeRooms[y-1][x].north.isGone == true && solveMaze(x, y-1))
		{
			return true;
		}
		if (y < height && mazeRooms[y+1][x].south.isGone == true && solveMaze(x, y+1))
		{
			return true;
		}
		//Remove mark from path if wrong path
		solution[y][x] = false;
		
		return false;
	}
	
	public void drawSolution()
	{
		int xCoordinate = 20;
		int yCoordinate = 20;
		
		//Pixel size of room- rooms are 20x20 pixels
		int roomSize = 20;

		int x = xCoordinate;
		int y = yCoordinate;

		for (int i = 0; i <= height - 1; i++) 
		{
			for (int j = 0; j <= width - 1; j++) 
			{
				if (solution[i][j]) 
				{
					//g.drawLine(x, y, x + roomSize, y);
					add(new SolveLineFigure(_view, x, y, x + roomSize, y));
				}
				x += roomSize;
			}
			x = xCoordinate;
			y += roomSize;
		}

	}
	
	

	@Override
	public void basicDisplayBox(Point origin, Point corner) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Rectangle displayBox() 
	{
		FigureEnumeration fe = figures();
		Rectangle r = fe.nextFigure().displayBox();

		while (fe.hasNextFigure()) 
		{
			r.add(fe.nextFigure().displayBox());
		}
		return r;
	}

	@Override
	public HandleEnumeration handles() 
	{
		List handles = CollectionsFactory.current().createList();
		handles.add(new GroupHandle(this, RelativeLocator.northWest()));
		handles.add(new GroupHandle(this, RelativeLocator.northEast()));
		handles.add(new GroupHandle(this, RelativeLocator.southWest()));
		handles.add(new GroupHandle(this, RelativeLocator.southEast()));
		return new HandleEnumerator(handles);
	}

}
